//
//  TFY_bottomToolView.m
//  TFY_PlayerView
//
//  Created by 田风有 on 2019/7/2.
//  Copyright © 2019 田风有. All rights reserved.
//

#import "TFY_bottomToolView.h"

@interface TFY_bottomToolView ()<SliderViewDelegate>
/// 播放或暂停按钮  /// 全屏按钮
@property (nonatomic, strong) UIButton *playOrPauseBtn,*fullScreenBtn,*selection_btn;
/// 播放的当前时间 视频总时间
@property (nonatomic, strong) UILabel *currentTimeLabel,*totalTimeLabel;
/// 滑杆
@property (nonatomic, strong) TFY_SliderView *slider;

@end

@implementation TFY_bottomToolView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UIImage *image = [UIImage imageNamed:@"videoImages.bundle/bottom_shadow"];
        self.layer.contents = (id)image.CGImage;
        
        [self addSubview:self.playOrPauseBtn];
        
        [self addSubview:self.currentTimeLabel];
        
        [self addSubview:self.fullScreenBtn];
        
        [self addSubview:self.totalTimeLabel];
        
        [self addSubview:self.slider];
        
        [self addSubview:self.selection_btn];
        
        // 更新小屏幕布局
        [self SmallscreenLayer];
    }
    return self;
}
/**
 *  小屏幕布局
 */
-(void)SmallscreenLayer{
    self.playOrPauseBtn.tfy_LeftSpace(10).tfy_CenterY(0).tfy_size(40, 40);
    
    self.currentTimeLabel.tfy_LeftSpaceToView(0, self.playOrPauseBtn).tfy_CenterY(0).tfy_size(55, 25);
    
    self.fullScreenBtn.tfy_RightSpace(10).tfy_CenterY(0).tfy_size(40, 40);
    
    self.totalTimeLabel.tfy_RightSpaceToView(5, self.fullScreenBtn).tfy_CenterY(0).tfy_size(55, 25);
    
    self.slider.tfy_LeftSpaceToView(5, self.currentTimeLabel).tfy_CenterY(0).tfy_RightSpaceToView(5, self.totalTimeLabel).tfy_Height(25);
    
    self.selection_btn.hidden = YES;
}
/**
 *  全屏幕布局
 */
-(void)fullscreenLayer{
    self.slider.tfy_LeftSpace(10).tfy_CenterY(-15).tfy_RightSpace(10);
    
    self.selection_btn.hidden = NO;
    
    self.selection_btn.tfy_RightSpaceToView(5, self.totalTimeLabel).tfy_CenterY(0).tfy_size(40, 30);
    
}
-(void)setSelected:(BOOL)selected{
    _selected = selected;
    self.playOrPauseBtn.selected = _selected;
}


-(void)setCurrentTime:(NSTimeInterval)currentTime{
    _currentTime = currentTime;
    
    self.currentTimeLabel.tfy_text([TFY_CommonUtils convertSecond2Time:_currentTime]);
    
}

-(void)setTotalTime:(NSTimeInterval)totalTime{
    _totalTime = totalTime;
    
    self.totalTimeLabel.tfy_text([TFY_CommonUtils convertSecond2Time:_totalTime]);
}

/**
 *  判断是不是全屏
 */
-(void)fullScreenMode:(BOOL)fullScreenMode VideoCount:(NSInteger)count{
    if (fullScreenMode == YES && count > 1) {
        self.fullScreenBtn.selected = YES;
        [self fullscreenLayer];
    }
    else{
        self.fullScreenBtn.selected = NO;
        [self SmallscreenLayer];
    }
}

-(void)setRightbool:(BOOL)rightbool{
    _rightbool = rightbool;
    
    self.selection_btn.selected = _rightbool;
}

#pragma 滑块代理
/**
 *  滑块滑动开始
 */
- (void)sliderTouchBegan:(float)value{
    if ([self.delegate respondsToSelector:@selector(sliderTouchBegan:)]) {
        [self.delegate sliderTouchBegan:value];
    }
}
/**
 *  滑块滑动中
 */
- (void)sliderValueChanged:(float)value{
    if ([self.delegate respondsToSelector:@selector(sliderValueChanged)]) {
        [self.delegate sliderValueChanged:value];
    }
}
/**
 * 滑块滑动结束
 */
- (void)sliderTouchEnded:(float)value{
    if ([self.delegate respondsToSelector:@selector(sliderTouchEnded:)]) {
        [self.delegate sliderTouchEnded:value];
    }
}
/**
 *  滑杆点击
 */
- (void)sliderTapped:(float)value{
    if ([self.delegate respondsToSelector:@selector(sliderTapped:)]) {
        [self.delegate sliderTapped:value];
    }
}
//播放
-(void)player_btnClick:(UIButton *)btn{
    btn.selected = !btn.selected;
    if ([self.delegate respondsToSelector:@selector(controlBarSetPlayerPlay:)]) {
        [self.delegate controlBarSetPlayerPlay:btn.selected];
    }
}

//全屏
-(void)fullscreen_btnClick:(UIButton *)btn{
    btn.selected = !btn.selected;
    if ([self.delegate respondsToSelector:@selector(controlBarSetPlayerFullScreen:)]) {
        [self.delegate controlBarSetPlayerFullScreen:btn.selected];
    }
}

//选集
-(void)selection_btnClick:(UIButton *)btn{
    btn.selected = !btn.selected;
    if ([self.delegate respondsToSelector:@selector(selectionplayer:)]) {
        [self.delegate selectionplayer:btn.selected];
    }
}



-(UIButton *)playOrPauseBtn{
    if (!_playOrPauseBtn) {
        _playOrPauseBtn = tfy_button();
        _playOrPauseBtn.tfy_image(@"videoImages.bundle/Stop", UIControlStateNormal).tfy_image(@"videoImages.bundle/bottom_window", UIControlStateSelected).tfy_action(self, @selector(player_btnClick:));
    }
    return _playOrPauseBtn;
}


-(UILabel *)currentTimeLabel{
    if (!_currentTimeLabel) {
        _currentTimeLabel = tfy_label();
        _currentTimeLabel.tfy_text(@"00:00").tfy_textcolor(@"ffffff", 1).tfy_fontSize(13).tfy_alignment(1).tfy_adjustsWidth(YES);
    }
    return _currentTimeLabel;
}
-(UILabel *)totalTimeLabel{
    if (!_totalTimeLabel) {
        _totalTimeLabel = tfy_label();
        _totalTimeLabel.tfy_text(@"00:00").tfy_textcolor(@"ffffff", 1).tfy_fontSize(13).tfy_alignment(1).tfy_adjustsWidth(YES);
    }
    return _totalTimeLabel;
}
-(UIButton *)fullScreenBtn{
    if (!_fullScreenBtn) {
        _fullScreenBtn = tfy_button();
        _fullScreenBtn.tfy_image(@"videoImages.bundle/btn_zoom_in", UIControlStateNormal).tfy_image(@"videoImages.bundle/btn_zoom_out", UIControlStateSelected).tfy_action(self, @selector(fullscreen_btnClick:));
        
    }
    return _fullScreenBtn;
}
-(UIButton *)selection_btn{
    if (!_selection_btn) {
        _selection_btn = tfy_button();
        _selection_btn.tfy_title(@"选集", @"ffffff", 13).tfy_action(self, @selector(selection_btnClick:));
    }
    return _selection_btn;
}

/**
 *  滑块
 */
-(TFY_SliderView *)slider{
    if (!_slider) {
        _slider = [TFY_SliderView new];
        _slider.delegate = self;
        _slider.maximumTrackTintColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:0.8];
        _slider.bufferTrackTintColor  = [UIColor whiteColor];
        _slider.minimumTrackTintColor = [UIColor redColor];
        [_slider setThumbImage:[UIImage imageNamed:@"videoImages.bundle/thumb_light"] forState:UIControlStateNormal];
        _slider.sliderHeight = 2;
    }
    return _slider;
}
@end
